﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace UpdateServers.Command
{
    /// <summary>
    /// 配制文件管理
    /// </summary>
    public static class ConfigManager
    {
        private static IConfiguration configuration;
        static ConfigManager()
        {
            var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                    .AddEnvironmentVariables();
            configuration = builder.Build();

        }
        /// <summary>
        /// 获取appsetting配制
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static string GetAppSetting(string key)
        {

            var result = string.Empty;
            if (!string.IsNullOrEmpty(key))
            {
                result = configuration.GetSection("AppSettings:" + key).Value;
            }
            return result;
        }
        /// <summary>
        /// 获取连接串
        /// </summary>
        /// <param name="name">连接串名</param>
        /// <returns></returns>
        public static string GetConnectionString(string name)
        {

            var result = string.Empty;
            result = configuration.GetConnectionString(name);
            return result;
        }
        /// <summary>
        /// 获取任意节点，多节点可用“:”分割
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static IConfigurationSection GetSection(string key)
        {

            return configuration.GetSection(key);
        }
        /// <summary>
        /// 返回泛型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetSection<T>(string key)
        {

            return configuration.GetSection(key).Get<T>();
        }
    }
}
